package com.decent.controller;

import com.decent.entity.Product;
import com.decent.entity.common.Message;
import com.decent.entity.common.PageResult;
import com.decent.service.ProductService;
import com.decent.enums.MessageEnum;
import com.decent.enums.MessageUtilEnum;
import com.decent.util.MessageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @Author decent
 * @Create 2021/4/7 - 17:06
 * @Description
 */
@RestController
@Validated
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 获取热门产品
     * @return Message
     */
    @RequestMapping("/product/hot")
    public Message listProductHot() {
        List<Product> hotProduct = productService.listHotProduct();

        return new Message(MessageEnum.GENERAL_REQUEST_SUCCESS, hotProduct);
    }

    /**
     * 获取产品列表
     * @param brandId 品牌ID
     * @param haveStock 仅看有货
     * @param sortType 排序种类
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @return Message
     */
    @RequestMapping("/product/list")
    public Message listProduct(@NotNull(message = "品牌id为空") @Min(1) Long brandId,
                               @RequestParam(defaultValue = "true") Boolean haveStock,
                               @RequestParam(defaultValue = "PRICE_BOTTOM_TO_HIGH") String sortType,
                               @RequestParam(defaultValue = "1") @Min(1)  Integer pageNumber,
                               @RequestParam(defaultValue = "6") @Min(1) Integer pageSize) {
        PageInfo<Product> pageInfo = productService.listProduct(brandId, haveStock, sortType, pageNumber, pageSize);
        PageResult pageResult = new PageResult(pageInfo);
        return MessageUtils.buildMsg(Objects.nonNull(pageInfo.getList()), MessageUtilEnum.PRODUCT_QUERY, pageResult);
    }

    /**
     * 获取产品详情
     * @param productId 产品ID
     * @return Message
     */
    @RequestMapping("/product/details/{productId}")
    public Message productDetailsByProductId(@PathVariable @NotNull(message = "产品id为空") @Min(1)  Long productId) {

        Product product = productService.productDetailsByProductId(productId);
        return MessageUtils.buildMsg(Objects.nonNull(product), MessageUtilEnum.PRODUCT_QUERY, product);
    }

}
